package com.wzdigit.wms.wms.controller.task;

import cn.hutool.core.collection.CollectionUtil;
import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.mes.basic.api.BasicDataItemDetailApi;
import com.wzdigit.mes.basic.dto.DataItemDetailDTO;
import com.wzdigit.mes.qms.dto.WipRcardCompletionDataDto;
import com.wzdigit.wms.basic.mapper.core.wip.WipGoodsMapper;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.dto.inventory.GoodsDto;
import com.wzdigit.wms.wms.task.IfGoodsSyncService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by zyf on 2022/1/5 9:16
 */
@Slf4j
@RestController
@Api(tags = "WMS下线同步Goods")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class GoodsSyncHandController {

    public static final String REQUEST_MODEL = "/hand";

    @Autowired
    private WipGoodsMapper wipGoodsMapper;

    @Autowired
    private IfGoodsSyncService ifGoodsSyncService;

    @Reference(version = "${dubbo.consumers.basic.data.item.detail.api:1.0.0}", check = false, timeout = 10000)
    BasicDataItemDetailApi basicDataItemDetailApi;

    @Value(value = "${default_owner.code}")
    private String DEFAULT_OWNER;

    @Value(value = "${default_owner.name}")
    private String DEFAULT_OWNER_NAME;

    @ApiOperation(value = "手动补偿", notes = "手动补偿")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/syncGoods")
    public void syncHand(HttpServletRequest servletRequest,
                                                HttpServletResponse servletResponse) {
        try {
            List<WipRcardCompletionDataDto> dtoDatas = wipGoodsMapper.searchSyncData();
            if (CollectionUtil.isEmpty(dtoDatas)){
                return;
            }
            log.info(">>>>>>>>>>Goods信息同步开始<<<<<<<<<<");
            log.info(dtoDatas.toString());
            //同步成功的DATA_ID集合
            DataItemDetailDTO dataItem = basicDataItemDetailApi.getDataItemDetailByPCodeAndName(DEFAULT_OWNER, DEFAULT_OWNER_NAME);
            for (WipRcardCompletionDataDto dto : dtoDatas) {
                try {
                    GoodsDto goodsDto=new GoodsDto();
                    goodsDto.setQaStatus(0);//0是待检

                    goodsDto.setQty(new BigDecimal(1));
                    goodsDto.setStatus(0);//未完成
                    goodsDto.setIsHold(0);//非冻结
                    goodsDto.setIsOriginalPackage(1);//原包装

                    goodsDto.setOwner(dataItem == null || dataItem.getItemValue() == null ? 10001 : Integer.parseInt(dataItem.getItemValue()));
                    goodsDto.setGoodsSn(dto.getRcardNo());
                    goodsDto.setSkuCode(dto.getProductCode());//产品编码-》SKU_CODE
                    goodsDto.setSoNo(dto.getSo());
                    goodsDto.setSoItem(dto.getSoItem());
                    goodsDto.setSoType(dto.getSoType());//销售订单类型
                    goodsDto.setPoNo(dto.getPo());//PO
                    Integer toSellFlag = dto.getToSellFlag();
                    if (toSellFlag != null) {
                        goodsDto.setSellFlag(toSellFlag.toString());//内外销标识
                    }
                    goodsDto.setFactory(dto.getFactoryCode());//组织 5500
                    goodsDto.setMfgPlant(dto.getFactoryCodeWorkShop());//分厂 一厂/二厂

                    goodsDto.setPrdWo(dto.getOrderCode());//工单
                    goodsDto.setPrdDate(dto.getCtime());
                    goodsDto.setInspNo(dto.getQcorderCode());//检验单号
                    goodsDto.setMfgLine(dto.getWorkLineCode());//产线
                    ifGoodsSyncService.syncData(goodsDto,dto.getDataId());
                }catch (Exception e){
                    log.info("---------["+dto.getRcardNo()+"]同步失败："+e.getMessage()+"---------");
                }
            }
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

}
